package day06;

import java.util.Arrays;
import java.util.Random;

//方法的演示
public class MethodDemo {
    public static void main(String[] args) {
        say();          //调用say（）方法
        System.out.println("--------------------");

        //sayHi();      //编译错误，有参则必须传参
        //sayHi(250);   //编译错误，参数类型必须匹配
        sayHi("ljt");//String name="ljt"
        System.out.println("--------------------");

        sayHello("cy",10);//参数必须都要填写
        System.out.println("--------------------");

        sayHello("wxy",90);//参数必须都要填写
        System.out.println("--------------------");

        int num = sum(5,6); //sum(5,6)的值就是return后的那个数
        System.out.println(num);
        int m = 7,n= 8;
        int num2 = sum(m,n);    //传的是m、n的值
        System.out.println(num2);
        System.out.println("--------------------");

        int[] a = generateArray(5,20);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("第一个元素为："+a[0]);
        System.out.println("--------------------");

        Arrays.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("第一个元素为："+a[0]);
        System.out.println("--------------------");

        int[] b = generateArray(3,10);
        System.out.println("数组的长度为："+b.length);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
        System.out.println("--------------------");
        say();
        say("张三");
        say("李四",50);
    }
    //无参数无返回值
    public static void say(){
        System.out.println("Hello World");
    }
    //有参数无返回值
    public static void sayHi(String name){
        System.out.println("大家好，我叫"+name+"今年20岁了");
    }
    //有参数无返回值
    public static void sayHello(String name,int age){
        if(age >= 60){
            return;
        }
        System.out.println("大家好，我叫"+name+"今年"+age+"岁了");
    }
    //有参数无返回值
    public static void say(String name){
        System.out.println("大家好，我叫"+name+"今年20岁了");
    }
    //有参数无返回值
    public static void say(String name,int age){
        if(age >= 60){
            return;
        }
        System.out.println("大家好，我叫"+name+"今年"+age+"岁了");
    }
    public static void say(int age){}               //正确的重载
    public static void say(int age,String name){}   //正确的重载
    //public static int say(){}                     //编译错误，重载与返回类型无关
    //public static void say(String address){}      //编译错误，重载与参数名称无关
    //有参数有返回值
    public static int sum(int a,int b){
        int c=a+b;
        return c;       //返回的是num里面的数
        //return a+b;   //返回的是a+b的值
    }
    //生成一个整型数组，并填充随机数据
    public static int[] generateArray(int len,int max){
        int[] a=new int[len];
        Random rand = new Random();
        for(int i=0;i<a.length;i++){
            a[i]=rand.nextInt(max);
        }
        return a;
    }
}
